using System;
using System.Web.UI;
using System.Collections;
using System.IO;
using System.Xml;

using ECCIT.Configuration;

namespace ECCIT.Web.Configuration
{
	/// <summary>
	/// 
	/// </summary>
	public class JavaScriptManager : BaseLinkFileManager
	{
        public static Hashtable Scripts = LoadScripts();

		private const string CacheKey = "Cache_ClientScript";

        public JavaScriptManager() { }

        public JavaScriptManager(string appVirtualPath)
            : base(appVirtualPath, "JavaScript", "*.js")
        { }
		

		public static Hashtable LoadScripts()
		{			
			string appPath = RequestContext.GetApplicationPhysicalPath();
			string filePath = Path.Combine(Path.Combine(appPath, "configuration"), "ClientScriptManager.config");
			Hashtable scriptsTable = InitializeScriptTable(filePath);
			return scriptsTable;
		}

		private static Hashtable InitializeScriptTable(string filePath)
		{
			Hashtable scriptsTable = null;
			if (File.Exists(filePath))
			{
				scriptsTable = new Hashtable();

				XmlDocument doc = new XmlDocument();
				doc.Load(filePath);

				XmlNodeList scriptNodes = doc.DocumentElement.SelectNodes("./script");	// get <script> node
				string key = null;
				XmlNodeList itemNodes = null;
				string[] itemPaths = null;
				foreach (XmlNode scriptNode in scriptNodes)
				{
					key = scriptNode.Attributes.GetNamedItem("key").InnerText;
					itemNodes = scriptNode.SelectNodes("./item");						// get <script><item> node
					if (itemNodes != null)
					{
						itemPaths = new string[itemNodes.Count];
						int i = 0;
						foreach (XmlNode itemNode in itemNodes)
						{
							itemPaths[i] = itemNode.Attributes.GetNamedItem("path").InnerText;
							i++;
						}
					}

					if (key != null && key != string.Empty && itemPaths != null)
						if (!scriptsTable.ContainsKey(key))
							scriptsTable.Add(key, itemPaths);
				}
			}

			return scriptsTable;
		}

		public void Render(HtmlTextWriter writer, string appVirtualPath, string key)
		{
			if (!Scripts.ContainsKey(key))
				return;

			string[] itemPaths = (string[])Scripts[key];

			string dir = appVirtualPath;
			if( !dir.EndsWith("/") )
				dir += "/";

			string filePath = null;
			foreach (string path in itemPaths)
			{
				filePath = dir + path;
				writer.AddAttribute( "language", "javascript" );
				writer.AddAttribute( HtmlTextWriterAttribute.Src, filePath );
				writer.RenderBeginTag( HtmlTextWriterTag.Script );
				writer.RenderEndTag();
			}			
		}

		public override void Render(System.Web.UI.HtmlTextWriter writer)
		{
			string dir = this.VirtualPath;
			if( !dir.EndsWith("/") )
				dir += "/";
				
			foreach(object objFileName in this.FileList)
			{
				string fileName = dir + objFileName.ToString();
				writer.AddAttribute( "language", "javascript" );
				writer.AddAttribute( HtmlTextWriterAttribute.Src, fileName );
				writer.RenderBeginTag( HtmlTextWriterTag.Script );
				writer.RenderEndTag();
			}
		}
	}
}
